package com.shigu.example.test.create;

import com.opentae.core.mybatis.utils.DDL2Bean;

import java.io.File;
import java.io.FileFilter;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * Created by bugzy on 2017/8/12 0012.
 */
public class GenerateExampleUpgrade {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入扫描路径：");
        String pathName = scanner.nextLine();
        File path = new File(pathName);
        if (path.isDirectory()) {
            roll(path);
        }
    }

    private static void roll(File path) {
        File[] files = path.listFiles(file -> file.isDirectory() && !file.getName().startsWith(".") || file.getName().endsWith("Example.java"));
        for (File file : files) {
            String name = file.getName();
            if (file.isDirectory()) {
                if (name.equals("beans")) {
                    loadBeans(file.listFiles(bean -> bean.getName().endsWith(".class")));
                } else {
                    roll(file);
                }
            } else {
                String beanName = name.replace("Example.java", "");
                String pack = file.getPath().replace(File.separator, ".");
                int indexCom = pack.indexOf(".com.");
                if (indexCom != -1) {
                    pack = pack.substring(indexCom + 1, pack.lastIndexOf(".", pack.indexOf("Example")));
                }
                String beanClassName = pack.replace("examples", "beans.") + beanName;
                try {
                    Class<?> beanClass = Class.forName(beanClassName);
                    Map<String, String> fields = new HashMap<>();
                    for (Field field : beanClass.getDeclaredFields()) {
                        if (field.getModifiers() == Modifier.PRIVATE) {
                            fields.put(field.getName(), field.getType().getSimpleName());
                        }
                    }
                    String code = DDL2Bean.exampleCreate(pack, fields, beanName);
                    PrintWriter out = new PrintWriter(file);
                    out.println(code);
                    out.flush();
                    out.close();
                } catch (ClassNotFoundException | FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void loadBeans(File[] beans) {
        try {
            for (File bean : beans) {
                Method method = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
                method.setAccessible(true);
                URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
                // 将当前类路径加入到类加载器中
                method.invoke(classLoader, bean.toURI().toURL());

                String classPath = bean.getPath().replace(File.separator, ".");
                classPath = classPath.substring(classPath.indexOf(".com.") + 1, classPath.lastIndexOf("."));
                Class<?> beanClass = Class.forName(classPath);
                Map<String, String> fields = new HashMap<>();
                for (Field field : beanClass.getDeclaredFields()) {
                    if (field.getModifiers() == Modifier.PRIVATE) {
                        fields.put(field.getName(), field.getType().getSimpleName());
                    }
                }
                String beanName = beanClass.getSimpleName();
                String pack = beanClass.getName().replace("beans", "examples").replace("."+beanName, "");
                String code = DDL2Bean.exampleCreate(pack, fields, beanName);
                PrintWriter out = new PrintWriter(bean.getPath().replace("target"+ File.separator + "classes", "src"+File.separator+"main"+ File.separator + "java").replace(File.separator + "beans", File.separator + "examples").replace(".class", "") + "Example.java");
                out.println(code);
                out.flush();
                out.close();
            }
        } catch (NoSuchMethodException | IllegalAccessException | MalformedURLException | InvocationTargetException | ClassNotFoundException | FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
